package org.example.decorator;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.DataInputStream;
import java.io.FileInputStream;

/**
 * @author
 * @description 装饰者测试
 */
@SpringBootTest
public class CoffeeBar {


    @Test
    void test() {
        //装饰者模式下的订单：2份巧克力+一份牛奶的LongBlack
        //1、点了一份LongBlack
        Drink order = new LongBlack();
        System.out.println("order LongBlack费用：" + order.cost());
        System.out.println("order LongBlack订单描述：" + order.getDescription());
        //2、加了一份牛奶
        order = new Milk(order);
        System.out.println("order LongBlack加了一份牛奶费用：" + order.cost());
        System.out.println("order LongBlack加了一份牛奶订单描述：" + order.getDescription());
        //3、加了一份巧克力
        order = new Chocolate(order);
        System.out.println("order LongBlack加了一份牛奶 LongBlack加了一份巧克力费用：" + order.cost());
        System.out.println("order LongBlack加了一份牛奶 LongBlack加了一份巧克力订单描述：" + order.getDescription());
        //4、又加了一份巧克力
        order = new Chocolate(order);
        System.out.println("order LongBlack加了一份牛奶 LongBlack加了2份巧克力费用：" + order.cost());
        System.out.println("order LongBlack加了一份牛奶 LongBlack加了2份巧克力订单描述：" + order.getDescription());
    }

    @Test
    void test2() {
        Drink order2 = new Decaf();
        System.out.println("order2 无因咖啡费用：" + order2.cost());
        System.out.println("order2 无因咖啡订单描述：" + order2.getDescription());

        order2 = new Milk(order2);
        System.out.println("order 无因咖啡加了一份牛奶费用：" + order2.cost());
        System.out.println("order 无因咖啡加了一份牛奶订单描述：" + order2.getDescription());
    }

    @Test
    void test3() throws Exception {
        /**
         * 说明
         * 1.InputStream是抽象类，类似前面提到的Drink
         * 2.FileInputStream是InputStream的子类，类似前面提到的Decaf
         * 3.FilterInputStream是InputStream的子类，类似前面提到的Decorator修饰者
         * 4.DataInputStream是FileInputStream的子类，具体的修饰者，类似前面提到的Milk
         * 5.FilterInputStream类有protected volatile InputStream in;即含被装饰者
         * 6.分析得出在jdk的io体系中，就是使用装饰者模式
         */
        DataInputStream dis = new DataInputStream(new FileInputStream("d:abc.txt"));
        System.out.println(dis.read());
        dis.close();


    }
}